
<template>
    <el-form :inline="true" :model="formData" class="demo-form-inline">
        <el-form-item>
           <CategoryTree v-model="formData.parentId" ></CategoryTree>
        </el-form-item>
        <el-form-item>
            <el-input v-model="formData.name" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">

        <el-table-column fixed prop="id" label="编号" width="60" />
        <el-table-column fixed prop="name" label="名称" width="120" />
        <el-table-column label="图片" width="120">
            <template #default="scope">
                <img :src="scope.row.img" width="50" height="50" />
            </template>
        </el-table-column>
    
        <el-table-column prop="lastUpdateBy" label="更新人" width="120" :formatter="formatter" />
        <el-table-column prop="lastUpdateTime" label="更新时间" width="160" :formatter="formatter" />

        <el-table-column fixed="right" label="操作" min-width="200">
            <template #default="scope">
                <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleClick(scope.row)">删除</el-button>

            </template>
        </el-table-column>
    </el-table>

    <p></p>

    <el-pagination background layout="total, prev, pager, next" :total="pageData.total" :page-size="pageData.pageSize"
   @current-change="pageChange">
    </el-pagination>

</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { categoryApi, } from '@/api'
import { dayjs, ElMessageBox, ElMessage } from 'element-plus'
import CategoryTree from '@/components/CategoryTree.vue';
import {useRouter,useRoute} from 'vue-router'
const router =useRouter()
const route = useRoute()
const edit = (row: any) => {
    router.push({name: 'category.edit', query: { id: row.id }})

}
const tableData = ref([])


const formData = reactive({
    name: '',
    parentId: '',
})
const pageData = reactive({
    total: 0,
    pageSize: 100,
    pageNum: 1
})

onMounted(() => {
    callApi()

})



const onSubmit = () => {
    callApi()
}
const callApi= () => {

    let params = {
        pageNum: pageData.pageNum,
        pageSize: pageData.pageSize,
        name: formData.name == "" ? undefined : formData.name,
        parentId:formData.parentId
        
    }

    categoryApi.select.call(params).then((res) => {

        pageData.total = res.count
        tableData.value = res.items
    })

}
const pageChange = (pageNum: number) => {

    pageData.pageNum = pageNum
    callApi()

}
const formatter = (row: any, column: any, cellValue: any, index: number) => {

    if (column.property === 'lastUpdateTime') {
        return dayjs(cellValue).format('YY年MM月DD HH:mm')
    } else if (column.property === 'lastUpdateBy') {
        return cellValue
    }

    return cellValue
}

const handleClick = (row: any) => {

  

    ElMessageBox.confirm(
        '确认删除'+row.name+'?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            categoryApi.delete.call({id:row.id}).then((res) => {
                callApi()
                ElMessage.success('删除成功')
                

            })
        

        })
        .catch(() => {

        })
}

</script>